import * as actions from '../actions/index';
import qs from 'qs';

function getInitialState() {
  const params = qs.parse(window.location.search.substr(1));

  // 页面初始化数据
  const initialState = {
    commits: [],
    curCommitInfo: {},
    isLoadingList: false,
    filterText: '',
    curCommitId: '',
    from: params.from || '',
    to: params.to || '',
    tab: params.tab || 'commits',
    isShowCommits: true,
    isHideTip: false,
    fromBranch: '',
    toBranch: '',
    // comparisonValue: 'Merge comparison',
    comparisonValue: 'Merge comparison',
  };

  return initialState;
}

const defaultAction = {
  type: 'doNothing',
};

export default function index(state = getInitialState(), action = defaultAction) {
  switch (action.type) {
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.UPDATE_PAGE_INFO:
      return getInitialState();
    case actions.COMPARISON_CHANGE:
      return { ...state, comparisonValue: action.data };
    default:
      return state;
  }
}
